import util.ReadLineUtil;
import util.StringUtil;

import java.util.ArrayList;

/**
 * Created by gamy on 2017/7/15.
 */
public class SQLParser {

    private final String fileName;
    private String tableName;

    SQLParser(String fileName){
        this.fileName = fileName;
    }

    ArrayList<TableProperty> parse(){
        ArrayList<TableProperty> list = new ArrayList<TableProperty>();

        ReadLineUtil.readLine(this.fileName, new ReadLineUtil.ReadLineHandler() {

            TableProperty curTP = null;
            @Override
            public void begin() {

            }

            @Override
            public void handle(String lineStr) {
                String name = StringUtil.getTableName(lineStr);
                if (name != null){
                    tableName = name;
                    TableProperty tp = new TableProperty(name);
                    tp.tableName = tableName;
                    curTP = tp;
                    list.add(tp);
                }else {
                    name = StringUtil.getColumnName(lineStr);
                    if (name != null && tableName != null && curTP != null){
                        TableProperty.TableColumn column = new TableProperty.TableColumn(lineStr);
                        curTP.columns.add(column);
                    }
                }
            }

            @Override
            public void end() {
//                System.out.println(list);
            }
        });

        return list;
    };


/*
    private static String getType(String str) {
        return TableProperty.covertType(str);
    }
*/





/*

    private static String handleColumnName(String str, boolean isID) {
        if (isID) {
            //如果是主键
            return "    <id property=\"" + changeIntoProperty(str) + "\" column=\"" + str + "\"/>";
        } else {
            return "    <result property=\"" + changeIntoProperty(str) + "\" column=\"" + str + "\"/>";
        }
    }
*/

//    private static String changeIntoProperty(String str) {
//        return underline2Camel(str, true);
//    }

//    private static String handleTableNameEnd(String str) {
//        return "</resultMap>\n";
//    }


}
